﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ShootSearch.Jobs;
using ShootSearch.Parsers;
using ShootSearch.Util.Serializers;
using ShootSearch.Core;
using ShootSearch.FileSystem;
using ShootSearch.Spiders;
using System.Xml.Serialization;
using ShootSearch.Common;
using System.Diagnostics;

namespace ShootSearch.Tests.Parsers
{
    [TestFixture]
    public class ParserTests
    {
        public static void Run()
        {
            ParserTests obj = new ParserTests();
        }


        [Test]
        public ParserJobConfig GenerateParser()
        {
            ParserJobConfig config = new ParserJobConfig();

            config.Parsers.Add(new ShootSearch.Core.AssemblyWithConfig(typeof(HtmlParser)));
            config.Parsers.Add(new ShootSearch.Core.AssemblyWithConfig(typeof(DocParser)));

            DocParserConfig doc = config.Parsers[1].Config as DocParserConfig;

            if (doc != null)
            {
                doc.ContentTypeToExtensions.Add("application/msword", ".doc");
                doc.ContentTypeToExtensions.Add("application/msexcel", ".xls");
                doc.ContentTypeToExtensions.Add("application/mspowerpoint", ".ppt");

                doc.ContentTypeToExtensions.Add("application/vnd.ms-word", ".doc");
                doc.ContentTypeToExtensions.Add("application/vnd.ms-excel", ".xls");
                doc.ContentTypeToExtensions.Add("application/vnd.ms-powerpoint", ".ppt");

                doc.ContentTypeToExtensions.Add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".docx");
                doc.ContentTypeToExtensions.Add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx");
                doc.ContentTypeToExtensions.Add("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx");


                doc.ContentTypeToExtensions.Add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", ".dotx");
                doc.ContentTypeToExtensions.Add("application/vnd.openxmlformats-officedocument.presentationml.template", ".potx");
                doc.ContentTypeToExtensions.Add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", ".xltx");

                doc.ContentTypeToExtensions.Add("application/vnd.ms-word.document.macroEnabled.12", ".docm");
                doc.ContentTypeToExtensions.Add("application/vnd.ms-powerpoint.template.macroEnabled.12", ".potm");
                doc.ContentTypeToExtensions.Add("application/vnd.ms-excel.template.macroEnabled.12", ".xltm");

                doc.ContentTypeToExtensions.Add("application/pdf", ".pdf");
                doc.ContentTypeToExtensions.Add("application/x-httpd-php", ".php");
                doc.ContentTypeToExtensions.Add("application/mshelp", ".chm");
            }

            XmlFormatter.ToXmlFile(config, "ParserJob.config");

            return config;   
        }

        [Test]
        public void ParserConfigTest()
        {
            ParserJobConfig expected = GenerateParser();
            expected.InputArgument.Path = "xxx";
            string xml = XmlFormatter.ToXml(expected);

            ParserJobConfig actual = XmlFormatter.FromXml(xml, expected.GetType()) as ParserJobConfig;

            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(expected.InputArgument.Path, actual.InputArgument.Path);

            Assert.AreEqual(expected.Parsers.Count, actual.Parsers.Count);
        }

        [Test]
        public void RegexParserTest()
        {
            ParserJobConfig pc = new ParserJobConfig();
            pc.Parsers.Add(new AssemblyWithConfig(typeof(RegexParser)));
            RegexParserConfig rc = pc.Parsers[0].Config as RegexParserConfig;

            PageRule pr = new PageRule();
            pr.Type = PageRuleType.UrlOnly;
            pr.CanContinue = false;
            pr.Rule = new TreeRule("http://www.cnblogs.com/eaglet/archive/\\d{4}/\\d{2}/\\d{2}/\\d+\\.html");

            TreeRule title = new TreeRule("<title>(?<Title>.*) - eaglet - 博客园</title>");
            title.GroupName = "Title";

            TreeRule content = new TreeRule("<div class=\"postbody\">(?<Content>(.|\\n)*)<div id=\"EntryTag\">");
            content.GroupName = "Content";

            pr.Rule.Children.Add(title);
            pr.Rule.Children.Add(content);

            rc.Rules.Add(pr);

            string xml = XmlFormatter.ToXml(pc);
            Debug.WriteLine(xml);

        }


    }

  
}
